package com.zuowei.ycy.user.ui.mine;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.and.yzy.frame.crop.CropHelper;
import com.and.yzy.frame.crop.CropParams;
import com.and.yzy.frame.util.RetrofitUtils;
import com.and.yzy.frame.view.dialog.FormBotomDialogBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.http.Member;
import com.zuowei.ycy.ui.BasePhotoCropActivity;
import com.zuowei.ycy.user.util.AppJsonUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/4/19.
 */
public class MineDataAty extends BasePhotoCropActivity {

    @Bind(R.id.imgv_head)
    SimpleDraweeView imgvHead;
    @Bind(R.id.edit_nickname)
    EditText editNickname;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.linerly_account_pwd)
    LinearLayout linerlyAccountPwd;
    private CropParams cropParams;

    private File file;

    @Override
    public int getLayoutId() {
        return R.layout.mine_data_aty;
    }

    @Override
    public void initData() {
        mToolbar.setTitle("个人资料");

        cropParams = new CropParams(this);
        cropParams.outputX = (int) getResources().getDimension(R.dimen.x200);
        cropParams.outputY = (int) getResources().getDimension(R.dimen.x200);

        if (UserManger.getUserInfo().getType().equals("1")) {
            linerlyAccountPwd.setVisibility(View.VISIBLE);
        } else {
            linerlyAccountPwd.setVisibility(View.GONE);
        }

        showData();
    }

    private void showData() {
        imgvHead.setImageURI(Uri.parse(UserManger.getUserInfo().getHead_pic()));
        editNickname.setText(UserManger.getUserInfo().getNickname());
        if (UserManger.getUserInfo().getType().equals("1")) {
            String phone = UserManger.getUserInfo().getAccount().substring(0, 3) + "****" + UserManger.getUserInfo().getAccount().substring(7, 11);
            tvPhone.setText(phone);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (file != null) {
            file.delete();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_save, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_save) {
            String name = editNickname.getText().toString();
            if (TextUtils.isEmpty(name)) {
                showToast("昵称不能为空");
                return true;
            }

            showLoadingDialog(null);
            //            doHttp(RetrofitUtils.createApi(Member.class).modifyBaseData(UserManger.getM_id(), name, RequestBody.create(MediaType.parse("\"image/*\""), file)), 0);

            Map<String, RequestBody> map = new HashMap<>();
            RequestBody body1 = RequestBody.create(MediaType.parse("text/plain"), UserManger.getM_id());
            RequestBody body2 = RequestBody.create(MediaType.parse("text/plain"), name);
            map.put("m_id", body1);
            map.put("nickname", body2);
            if (file != null) {
                RequestBody body3 = RequestBody.create(MediaType.parse("image/*"), file);
                map.put("head_pic\"; filename=\"" + file.getName() + "", body3);
            }
            //            showToast("修改成功");
            doHttp(RetrofitUtils.createApi(Member.class).modifyBaseData(map), 0);

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void requestData() {

    }

    @Override
    public void onPhotoCropped(Uri uri) {
        file = new File(uri.getPath());
        imgvHead.setImageURI(uri);
    }

    @Override
    public void onCompressed(Uri uri) {
        file = new File(uri.getPath());
        imgvHead.setImageURI(uri);

    }

    @Override
    public CropParams getCropParams() {
        return cropParams;
    }

    @OnClick({R.id.linerly_head, R.id.rl_update_phone, R.id.rl_update_pwd})
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.linerly_head:
                choosePhoto();
                break;
            case R.id.rl_update_phone:
                startActivityForResult(MineUpdatePhoneFirstAty.class, null, 1);
                break;
            case R.id.rl_update_pwd:
                startActivity(MineUpdatePwdAty.class, null);
                break;
            case R.id.rl_set_paypwd:
                startActivity(SettingPayPwdAty.class,null);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1 && requestCode == RESULT_OK) {
            String phone = UserManger.getUserInfo().getAccount().substring(0, 3) + "****" + UserManger.getUserInfo().getAccount().substring(7, 11);
            tvPhone.setText(phone);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void choosePhoto() {
        final FormBotomDialogBuilder builder = new FormBotomDialogBuilder(this);
        View v = getLayoutInflater().inflate(R.layout.choose_photo_layout, null);
        builder.setFB_AddCustomView(v);

        CropHelper.clearCachedCropFile(cropParams.uri);
        cropParams.refreshUri();
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.fbtn_one:
                        cropParams.enable = true;
                        cropParams.compress = true;
                        Intent intent1 = CropHelper.buildCameraIntent(cropParams);
                        startActivityForResult(intent1, CropHelper.REQUEST_CAMERA);
                        builder.dismiss();
                        break;
                    case R.id.fbtn_two:
                        cropParams.enable = true;
                        cropParams.compress = true;
                        Intent intent2 = CropHelper.buildGalleryIntent(cropParams);
                        startActivityForResult(intent2, CropHelper.REQUEST_CROP);
                        builder.dismiss();
                        break;
                    case R.id.fbtn_three:
                        builder.dismiss();
                        break;
                }
            }
        };

        v.findViewById(R.id.fbtn_one).setOnClickListener(listener);
        v.findViewById(R.id.fbtn_two).setOnClickListener(listener);
        v.findViewById(R.id.fbtn_three).setOnClickListener(listener);
        builder.show();
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        super.onSuccess(result, call, response, what);
        if (what == 0) {
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("uri", cropParams.uri);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            cropParams.uri = savedInstanceState.getParcelable("uri");

        }
    }
}
